<?php
namespace Manage\Model;
use Think\Model;
use Common\Library\Category;
class MenuModel extends Model{
	/**
	 * 获取菜单的列表
	 */
	public function getMenus($data=array()){
		$map=array(
			'language_id'	=>	get_default_lng_id()
			);
		if(isset($data['pid'])) $map['pid']=$data['pid'];
		if(isset($data['is_show'])) $map['is_show']=$data['is_show'];
		if(isset($data['menu_id'])) $map[C('DB_PREFIX').'menu.menu_id']=array('neq',$data['menu_id']);
		if(isset($data['status'])) $map['status']=$data['status'];
		if(isset($data['title'])) $map['title'] = array('like',"%".$data['title']."%");
		$order='sort_order ' . (isset($data['order'])?$data['order']:'ASC') . ',menu_id ASC';
		$menues=$this->join('LEFT JOIN  __MENU_DESCRIPTION__ ON __MENU__.menu_id=__MENU_DESCRIPTION__.menu_id')->where($map)->order($order)->field(C('DB_PREFIX')."menu.menu_id,title,url,pid,icon,is_show")->select();
		return $menues;
	}
 	/**
 	 * 新增/修改后台菜单
 	 */
	public function viewMenu($menu_id=0,$data){
		$menu_array=array(
				'url'	=>	$data['url'],
				'pid'	=>	$data['pid'],
				'status'=>	$data['status'],
				'sort_order'=>$data['sort_order'],
				'icon'	=>	$data['icon'],
				);
		$this->create($data);
		if($menu_id){			
			$map=array(
				'menu_id'=>$menu_id
				);			
			$this->where($map)->save();
		}else{
			$menu_id=$this->add();
			$map=array(
				'menu_id'=>$menu_id
				);
		}	
		$MenuDescriptionModel=M('MenuDescription');
		$MenuDescriptionModel->where($map)->delete();
		$data_description=array();		
		foreach ($data['menu_description'] as $language_id => $description) {
			$data_description=array(
				'language_id'	=>	$language_id,
				'menu_id'		=>	$menu_id,
				'title'			=>	$description['title']
				);
			$MenuDescriptionModel->create($data_description);
				$MenuDescriptionModel->add();
			}
		return $menu_id;		
	}
	public function getMenu($menu_id){
		$md=array();
		$map=array('menu_id'=>$menu_id);
		$menu=$this->where($map)->find();
		$menu_description=M('MenuDescription')->where($map)->select();
		foreach ($menu_description as $v) {
			$md[$v['language_id']]['title']=$v['title'];
		}
		$menu['menu_description']=$md;
		return $menu;
	}
	public function getHeadInfo($url=''){
		$url=$url?$url:CONTROLLER_NAME . "/" . ACTION_NAME;
		$return=array();
		$where['url']=array('LIKE',"%$url%");
		$menu=$this->where($where)->find();
		$menu_id=$menu['menu_id'];
		$map=array(
			'status'		=>	1,
			);
		$allmenu=$this->getMenus($map);
		$menuheader=Category::getParents($allmenu,$menu_id,'menu_id','pid');
		foreach($menuheader as $k=> $v){
			if(!$v['is_show']) unset($menuheader[$k]);
		}
		if($menuheader) {
			$return['heading_title']=$menuheader[count($menuheader)-1]['title'];
			$return['breadcrumb']=$menuheader;
		}

		return $return;
	}

	public function del($menu_id){
		$mids[]=$menu_id;
		$menus=$this->where('status=1')->select();
		$mids=array_merge($mids,Category::getChildsId($menus,$menu_id,'menu_id','pid'));
		$map['menu_id']=array('in',$mids);
		$return=$this->where($map)->delete();
		M('MenuDescription')->where($map)->delete();
		return $return;
	}


}